package engine.systems.motion.shake
{
	import engine.classes.enumerations.SystemPriority;
	import engine.groups.Group;
	import engine.systems.spatial.spatialOffset.SpatialOffsetData;
	import engine.systems.update.GameSystem;
	import engine.utils.GeomUtils;
	import engine.utils.MathUtils;
	
	import flash.geom.Point;
	
	public class ShakeSystem extends GameSystem
	{
		private const SHAKE:String = "shake";
		
		private var data:SpatialOffsetData;
		
		public function ShakeSystem()
		{
			super(ShakeNode, updateNode, nodeAdded, nodeRemoved);
			
			this.priority = SystemPriority.PRE_MOTION;
		}
		
		public function updateNode(node:ShakeNode, time:Number):void
		{
			if(node.shake.locked) return;
			
			node.shake.time += time;
			while(node.shake.time >= node.shake.wait)
			{
				node.shake.time -= node.shake.wait;
				
				data = node.offset.get(SHAKE);
				data.x = MathUtils.randomNumber(node.shake.rect.left, node.shake.rect.right);
				data.y = MathUtils.randomNumber(node.shake.rect.top, node.shake.rect.bottom);
			}
		}
		
		private function nodeAdded(node:ShakeNode):void
		{
			node.offset.add(SHAKE, new SpatialOffsetData());
		}
		
		private function nodeRemoved(node:ShakeNode):void
		{
			node.offset.remove(SHAKE);
		}
	}
}